//
//  CommentViewController.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 8/7/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "CommentViewController.h"
#import "PickFoodViewController.h"
#import "ListFoodViewController.h"
#import "FoodDetailViewController.h"
#import "ELCImagePickerController.h"
#import "ZooUtil.h"

@implementation CommentViewController
@synthesize arPic;

#define PADDING_SIZE 6
#define IMAGE_FOOD_SIZE 48

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithObject:(NSString *)a {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        arPic = [[NSMutableArray alloc]init];
        [self initHeaderView];
        [self initContent];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void) initHeaderView {
    _vHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 48)];
    _vHeader.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:IMG_NAV_BACKGROUND]];
    
    _btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
    [_btnBack setBackgroundImage:[UIImage imageNamed:IMG_BACK_NORMAL] forState:UIControlStateNormal];
    [_btnBack setBackgroundImage:[UIImage imageNamed:IMG_BACK_SELECTED] forState:UIControlStateHighlighted];
    [_btnBack addTarget:self action:@selector(backClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    _btnCheckIn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 3/4, 0, self.view.bounds.size.width/4, 48)];
//    [_btnCheckIn setBackgroundImage:[UIImage imageNamed:IMG_CHECKIN_NORMAL] forState:UIControlStateNormal];
//    [_btnCheckIn setBackgroundImage:[UIImage imageNamed:IMG_CHECKIN_SELECTED] forState:UIControlStateHighlighted];

    [_btnCheckIn addTarget:self action:@selector(pushPickFoodScreen) forControlEvents:UIControlEventTouchUpInside];
    
    [_btnCheckIn setTitle:STRING_BTN_CHECKIN forState:UIControlStateNormal];
    [_btnCheckIn setFont:[UIFont boldSystemFontOfSize:15]];
    
    [_vHeader addSubview:_btnBack];
    [_vHeader addSubview:_btnCheckIn];
    
    [self.view addSubview:_vHeader];
    
    [_btnBack release];
    [_btnCheckIn release];
    [_vHeader release];
}

- (void) initContent {
    _commentView = [[CommentScreenView alloc]initWithFrame:CGRectMake(0, 48, self.view.bounds.size.width, self.view.bounds.size.height - 48)];
    _commentView.scrollEnabled = NO;
    _commentView.parent = self;
    [_commentView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
    [self.view addSubview:_commentView];
    [_commentView release];
}

- (void)backClicked:(id)sender {
    
    [self dismissModalViewControllerAnimated:YES];
}

- (void)pushPickFoodScreen:(NSMutableArray *)arChecked arrayNoneCheck:(NSMutableArray *)arNoneCheck {
//    _pickFood = [[PickFoodViewController alloc]initWithObject:@"a"];
    _pickFood = [[PickFoodViewController alloc]initWithObject:arChecked arrayNoneChecked:arNoneCheck];
//    _pickFood.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    _pickFood.pickFoodViewDelegate = self;
    [self presentModalViewController:_pickFood animated:YES];

//    [ZooUtil pushController:self.navigationController toViewController:_pickFood transition:UIViewAnimationTransitionCurlDown];

    [_pickFood release];
}

- (void)pushViewPictureScreen {
    _viewPicture = [[ViewPictureViewController alloc]initWithObject:arPic];
    _viewPicture.viewPictureDelegate = self;
    [_viewPicture.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
    [self presentModalViewController:_viewPicture animated:YES];
}

#pragma mark - Image picker delegate
-(void)pickFoodViewControllerDismissed:(NSMutableArray *)arFoodChecked arrayNoneCheck:(NSMutableArray *)arFoodNoneCheck {
    _commentView.arFoodChecked = arFoodChecked;
    _commentView.arFoodNoneCheck = arFoodNoneCheck;
    [_commentView initScrollViewFood];
    
}

- (void)viewPictureControllerDismissed:(NSMutableArray *)arPicture {
    self.arPic = arPicture;
}

#pragma mark - Image picker delegate
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
//    [picker dismissModalViewControllerAnimated:YES];
//}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
    if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera )
        {
            _commentView.vImgFood.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
            UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
            [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
             {
                 NSLog(@"IMAGE SAVED TO PHOTO ALBUM");
                 [library assetForURL:assetURL resultBlock:^(ALAsset *asset )
                  {
          
                      NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
                      [workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
                  
                      [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
                  
                      [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];
                  
                      NSLog(@"%@",[asset valueForProperty:ALAssetPropertyURLs]);
                      [arPic addObject:workingDictionary];
                      [self dismissModalViewControllerAnimated:YES];
                    
                      NSLog(@"we have our ALAsset!");
                  } 
                 
                 failureBlock:^(NSError *error )
                  {
                  NSLog(@"Error loading asset");
                  [self dismissModalViewControllerAnimated:YES];
                  }];
             }];
        }  
    
}

#pragma mark - ELC Image picker delegate
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
    [self dismissModalViewControllerAnimated:YES];
    
    if (!self.arPic) {
        self.arPic = [NSMutableArray arrayWithArray:info];
    } else {
        [self.arPic addObjectsFromArray:info];
        NSLog(@"%d",[arPic count]);
        if ([arPic count]>3) {
            for (int i=0; i<3; i++) {
                NSMutableDictionary *picture = [arPic objectAtIndex:([arPic count]-i-1)];
                UIImageView *_vImgFood = [[UIImageView alloc]initWithFrame:CGRectMake(PADDING_SIZE-1, PADDING_SIZE*(i+1) + IMAGE_FOOD_SIZE*i, IMAGE_FOOD_SIZE, IMAGE_FOOD_SIZE)];            
                [_vImgFood setImage:[picture objectForKey:UIImagePickerControllerOriginalImage]];
                [_commentView.btnGallery  addSubview:_vImgFood];
                [_vImgFood release];
            }
        } else {
            for (int i=0; i<[arPic count]; i++) {
                NSMutableDictionary *picture = [arPic objectAtIndex:i];
                UIImageView *_vImgFood = [[UIImageView alloc]initWithFrame:CGRectMake(PADDING_SIZE-1, PADDING_SIZE*(i+1) + IMAGE_FOOD_SIZE*i, IMAGE_FOOD_SIZE, IMAGE_FOOD_SIZE)];            
                [_vImgFood setImage:[picture objectForKey:UIImagePickerControllerOriginalImage]];
                [_commentView.btnGallery  addSubview:_vImgFood];
                [_vImgFood release];
            }
        }
    }
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
